//
//  SwiftBaseTableViewController.swift
//  SafeFoodManagerDemo
//
//  Created by 振轩 on 2018/4/20.
//  Copyright © 2018年 bob. All rights reserved.
//





import UIKit



enum REQuestDataType:Int{
  case  NODATAType = 0
  case  NORMALLType = 1
    
}

class SwiftBaseTableViewController: UITableViewController {
    
    
    var Tpview:TypeView? = TypeView()
    
   
    
    var Datatype:REQuestDataType = REQuestDataType(rawValue: 0)!{
        didSet{
            
            if Datatype.rawValue == 0 {
                
                Tpview?.text = "没有任何数据"
                Tpview?.imageName = "emptynoData"
                self.tableView.addSubview(self.Tpview!)
            }
            
            if Datatype.rawValue == 1 {
                self.Tpview?.removeFromSuperview();
                
            }
           
            
            
        }
        
        
        
        
    }
    
    var Count:NSInteger = 0
    
    var currentPage:NSInteger = 0
    
    var PageSize:NSInteger = 20
    var isLoadmore:Bool = false
    //是否加载完毕。
    var isFinish:Bool = false
    
    let header = MJRefreshNormalHeader()
    // 底部加载
    let footer = MJRefreshAutoNormalFooter()
    
    

    override func viewDidLoad() {
        super.viewDidLoad()
        self.Tpview?.frame = self.tableView.bounds
        //    self.Tpview?.backgroundColor = UIColor.red

    
        header.setRefreshingTarget(self, refreshingAction: #selector(headerRefresh))
        self.tableView!.mj_header = header
        footer.setRefreshingTarget(self, refreshingAction: #selector(footerLoad))
        footer.isAutomaticallyRefresh = false
        self.tableView!.mj_footer = footer
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 0
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 0
    }
   func loadData(){
    
    
        
        
        
        
    }
    
    @objc func headerRefresh(){
        currentPage = 0
        print("下拉刷新.")
        isLoadmore = false
        sleep(1)
        //重现生成数据
        loadData()
        //重现加载表格数据
        self.tableView!.reloadData()
        //结束刷新
        self.tableView!.mj_header.endRefreshing()
        "已经加载"
        header.setTitle("已经加载"+"\(Count)"+"记录", for: MJRefreshState.refreshing)
    }
    @objc func footerLoad(){
        
       if self.isFinish {
            
          self.tableView.mj_footer.resetNoMoreData()
            
            
        }
        currentPage += 1
        isLoadmore = true
        print("上拉加载.")
        //sleep(1)
        //生成并添加数据
        loadData()
        //重现加载表格数据
        //self.tableView!.reloadData()
        //结束刷新
        self.tableView!.mj_footer.endRefreshing()
         footer.setTitle("已经加载"+"\(Count)"+"记录", for: MJRefreshState.refreshing)
    }
    

}
